<template>
  <el-row style="width: 100%">
    <el-col :span="20">{{ department.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex">
        <el-col>
          <span style="margin-right: 8px">{{ department.manager }}</span>
        </el-col>
        <el-col>
          <!-- 监听事件 触发方法 -->
          <el-dropdown @command="operation">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="新增">新增子部门</el-dropdown-item>
              <el-dropdown-item v-if="allowModify" command="编辑">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="allowModify" command="删除">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 导入删除部门的方法
import { deleteDepartments } from '@/api/departments'
export default {
  // 父子组件通信
  props: {
    // 接受父组件传递过来的department 要求是对象
    // eslint-disable-next-line
    department: Object,
    allowModify: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async  operation(arg) {
      switch (arg) {
        case '新增':
          this.$emit('add', this.department.id)
          break
        case '编辑':
          this.$emit('edit', this.department.id)
          break
        case '删除':
          // 确认删除弹窗
          await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          await deleteDepartments(this.department.id)
          // 提示删除成功
          this.$message('删除部门成功')
          // 向父组件发射事件重新刷新
          this.$emit('reload')
          break
        default:
          break
      }
    }
  }
}

</script>

<style lang="scss" scoped>
.el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
</style>
